home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZMISC.C < prev    next >
C/C++ Source or Header  |  1993-05-28  |  8KB  |  285 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zmisc.c */
  20. /* Miscellaneous operators for Ghostscript */
  21. #include "errno_.h"
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "gp.h"
  26. #include "errors.h"
  27. #include "oper.h"
  28. #include "alloc.h"
  29. #include "dict.h"
  30. #include "dstack.h"            /* for name lookup in bind */
  31. #include "iname.h"
  32. #include "ivmspace.h"
  33. #include "packed.h"
  34. #include "store.h"
  35. #include "gscrypt1.h"
  36.  
  37. /* Import the C getenv function. */
  38. extern char *getenv(P1(const char *));
  39.  
  40. /* Import the serial number from iinit.c. */
  41. extern const long gs_serialnumber;
  42.  
  43. /* <proc> bind <proc> */
  44. int
  45. zbind(register os_ptr op)
  46. {    os_ptr bsp = op;            /* bottom of stack */
  47.     ref *defp = op;
  48.     switch ( r_type(op) )
  49.        {
  50.     case t_array:
  51.     case t_mixedarray:
  52.     case t_shortarray:
  53.         break;
  54.     case t_oparray:
  55.         defp = &op_array_table.value.refs[op_index(op) - op_def_count];
  56.         break;
  57.     default:
  58.         return_error(e_typecheck);
  59.        }
  60.     ++bsp;
  61.     /* We must not make the top-level procedure read-only, */
  62.     /* but we must bind it even if it is read-only already. */
  63.     *bsp = *defp;
  64.     /* Here are the invariants for the following loop: */
  65.     /*    op < bsp <= ostop; */
  66.     /*    for every pointer p such that op < p <= bsp, */
  67.     /*      *p is an array (or packedarray) ref. */
  68. #define r_is_ex_oper(rp)\
  69.   ((r_btype(rp) == t_operator || r_type(rp) == t_oparray) &&\
  70.    r_has_attr(rp, a_executable))
  71.     while ( bsp > op )
  72.        {    while ( r_size(bsp) )
  73.            {    ref *tp = bsp->value.refs;
  74.             r_inc_size(bsp, -1);
  75.             if ( *(ushort *)tp > packed_max_full_ref )
  76.              { /* Check for a packed executable name */
  77.                ushort elt = *(ushort *)tp;
  78.                if ( (elt & ~(ushort)packed_max_name_index) ==
  79.                 pt_tag(pt_executable_name) )
  80.                 { ref nref;
  81.                   ref *pvalue;
  82.                   name_index_ref(elt & packed_max_name_index,
  83.                          &nref);
  84.                   if ( (pvalue = dict_find_name(&nref)) != 0 &&
  85.                    r_is_ex_oper(pvalue)
  86.                  )
  87.                 /* Note: can't undo this by restore! */
  88.                 *(ushort *)tp =
  89.                   pt_tag(pt_executable_operator) +
  90.                   op_index(pvalue);
  91.                 }
  92.                bsp->value.refs = (ref *)((ushort *)tp + 1);
  93.              }
  94.             else
  95.               switch ( bsp->value.refs++, r_type(tp) )
  96.              {
  97.             case t_name:    /* bind the name if an operator */
  98.               if ( r_has_attr(tp, a_executable) )
  99.                {    ref *pvalue;
  100.                 if ( (pvalue = dict_find_name(tp)) != 0 &&
  101.                      r_is_ex_oper(pvalue)
  102.                    )
  103.                     ref_assign_old(tp, pvalue, "bind");
  104.                }
  105.               break;
  106.             case t_array:    /* push into array if procedure */
  107.               if ( !r_has_attr(tp, a_write) ) break;
  108.             case t_mixedarray:
  109.             case t_shortarray:
  110.               if ( r_has_attr(tp, a_executable) && bsp < ostop )
  111.                {    /* Make reference read-only */
  112.                 r_clear_attrs(tp, a_write);
  113.                 *++bsp = *tp;
  114.                }
  115.              }
  116.            }
  117.         bsp--;
  118.        }
  119.     return 0;
  120. }
  121.  
  122. /* - currenttime <int> */
  123. int
  124. zcurrenttime(register os_ptr op)
  125. {    long date_time[2];
  126.     gp_get_clock(date_time);
  127.     push(1);
  128.     make_real(op, date_time[0] * 1440.0 + date_time[1] / 60000.0);
  129.     return 0;
  130. }
  131.  
  132. /* <string> getenv <value_string> */
  133. int
  134. zgetenv(register os_ptr op)
  135. {    char *str, *value;
  136.     int code;
  137.     check_read_type(*op, t_string);
  138.     str = ref_to_string(op, "getenv name");
  139.     if ( str == 0 )
  140.         return_error(e_VMerror);
  141.     value = getenv(str);
  142.     alloc_free(str, r_size(op) + 1, 1, "getenv name");
  143.     if ( value == 0 )        /* not found */
  144.        {    make_bool(op, 0);
  145.         return 0;
  146.        }
  147.     code = string_to_ref(value, op, "getenv value");
  148.     if ( code < 0 ) return code;
  149.     push(1);
  150.     make_bool(op, 1);
  151.     return 0;
  152. }
  153.  
  154. /* <name> <proc> .makeoperator <oper> */
  155. int
  156. zmakeoperator(register os_ptr op)
  157. {    check_type(op[-1], t_name);
  158.     check_proc(*op);
  159.     if ( r_is_local(op) )
  160.         return_error(e_invalidaccess);
  161.     if ( op_array_count == r_size(&op_array_table) )
  162.         return_error(e_limitcheck);
  163.     ref_assign_old(&op_array_table.value.refs[op_array_count],
  164.                op, "makeoperator");
  165.     op_array_nx_table[op_array_count] = name_index(op - 1);
  166.     r_set_type_attrs(op - 1, t_oparray, a_executable);
  167.     r_set_size(op - 1, op_def_count + op_array_count);
  168.     op_array_count++;
  169.     pop(1);
  170.     return 0;
  171. }
  172.  
  173. /* - .oserrno <int> */
  174. int
  175. zoserrno(register os_ptr op)
  176. {       push(1);
  177.     make_int(op, errno);
  178.     return 0;
  179. }
  180.  
  181. /* <int> .oserrorstring <string> */
  182. int
  183. zoserrorstring(register os_ptr op)
  184. {       int code;    
  185.     uint len;
  186.     byte ch;
  187.     check_type(*op, t_integer);
  188.     /****** ANSI C requires strerror, but many systems ******/
  189.     /****** don't support it.  Punt. ******/
  190.     code = string_to_ref("(error message not available)"
  191.                  /*strerror((int)op->value.intval)*/,
  192.                  op, "oserror");
  193.     if ( code < 0 )
  194.         return code;
  195.     /* Strip trailing end-of-line characters. */
  196.     while ( (len = r_size(op)) != 0 &&
  197.         ((ch = op->value.bytes[--len]) == '\r' || ch == '\n')
  198.           )
  199.         r_inc_size(op, -1);
  200.     return 0;
  201. }
  202.  
  203. /* - serialnumber <int> */
  204. int
  205. zserialnumber(register os_ptr op)
  206. {    push(1);
  207.     make_int(op, gs_serialnumber);
  208.     return 0;
  209. }
  210.  
  211. /* <string> <bool> .setdebug - */
  212. int
  213. zsetdebug(register os_ptr op)
  214. {    check_read_type(op[-1], t_string);
  215.     check_type(*op, t_boolean);
  216.        {    int i;
  217.         for ( i = 0; i < r_size(op - 1); i++ )
  218.             gs_debug[op[-1].value.bytes[i] & 127] =
  219.                 op->value.index;
  220.        }
  221.     pop(2);
  222.     return 0;
  223. }
  224.  
  225. /* <state> <from_string> <to_string> type1encrypt <new_state> <substring> */
  226. /* <state> <from_string> <to_string> type1decrypt <new_state> <substring> */
  227. private int type1crypt(P2(os_ptr,
  228.               int (*)(P4(byte *, const byte *, uint, ushort *))));
  229. int
  230. ztype1encrypt(os_ptr op)
  231. {    return type1crypt(op, gs_type1_encrypt);
  232. }
  233. int
  234. ztype1decrypt(os_ptr op)
  235. {    return type1crypt(op, gs_type1_decrypt);
  236. }
  237. private int
  238. type1crypt(register os_ptr op, int (*proc)(P4(byte *, const byte *, uint, ushort *)))
  239. {    crypt_state state;
  240.     uint ssize;
  241.     check_type(op[-2], t_integer);
  242.     state = op[-2].value.intval;
  243.     if ( op[-2].value.intval != state )
  244.         return_error(e_rangecheck);    /* state value was truncated */
  245.     check_read_type(op[-1], t_string);
  246.     check_write_type(*op, t_string);
  247.     ssize = r_size(op - 1);
  248.     if ( r_size(op) < ssize )
  249.         return_error(e_rangecheck);
  250.     (void) (*proc)(op->value.bytes, op[-1].value.const_bytes, ssize,
  251.                &state);        /* can't fail */
  252.     op[-2].value.intval = state;
  253.     op[-1] = *op;
  254.     r_set_size(op - 1, ssize);
  255.     pop(1);
  256.     return 0;
  257. }
  258.  
  259. /* - usertime <int> */
  260. int
  261. zusertime(register os_ptr op)
  262. {    long date_time[2];
  263.     gp_get_clock(date_time);
  264.     push(1);
  265.     make_int(op, date_time[0] * 86400000L + date_time[1]);
  266.     return 0;
  267. }
  268.  
  269. /* ------ Initialization procedure ------ */
  270.  
  271. op_def zmisc_op_defs[] = {
  272.     {"1bind", zbind},
  273.     {"0currenttime", zcurrenttime},
  274.     {"1getenv", zgetenv},
  275.     {"2.makeoperator", zmakeoperator},
  276.     {"0.oserrno", zoserrno},
  277.     {"1.oserrorstring", zoserrorstring},
  278.     {"1serialnumber", zserialnumber},
  279.     {"2.setdebug", zsetdebug},
  280.     {"3.type1encrypt", ztype1encrypt},
  281.     {"3.type1decrypt", ztype1decrypt},
  282.     {"0usertime", zusertime},
  283.     op_def_end(0)
  284. };
  285.